package com.thinksns.sociax.t4.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.android.img.ActivityViewPager;
import com.thinksns.sociax.t4.model.ModelPhoto;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhiyicx on 2017/5/16.
 */

public class AdapterEventBigImageList extends RecyclerView.Adapter<BigImageViewHolder> {

    private List<String> imgList;
    private Context context;

    public AdapterEventBigImageList(Context context, List<String> imgList) {
        this.context = context;
        this.imgList = imgList;
        if (this.imgList == null) {
            this.imgList = new ArrayList<>();
        }
    }

    public void resetData(List<String> imgList) {
        this.imgList.clear();
        this.imgList.addAll(imgList);
        notifyDataSetChanged();
    }

    public List<ModelPhoto> buildModelPhoto() {
        List<ModelPhoto> photos = new ArrayList<>();
        for (int i = 0; i < imgList.size(); i++) {
            ModelPhoto photo = new ModelPhoto();
            photo.setUrl(imgList.get(i));
            photo.setOriUrl(imgList.get(i));
            photo.setMiddleUrl(imgList.get(i));
            photos.add(photo);
        }
        return photos;
    }

    @Override
    public BigImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_event_big_image, null);
        return new BigImageViewHolder(view, context);
    }

    @Override
    public void onBindViewHolder(final BigImageViewHolder holder, int position) {
        Glide.with(context)
                .load(imgList.get(position))
                .placeholder(R.drawable.image750x574)
                .error(R.drawable.image750x574)
                .into(holder.bigImg);

        holder.bigImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(context, ActivityViewPager.class);
                i.putExtra("index", holder.getAdapterPosition());
                i.putParcelableArrayListExtra("photolist", (ArrayList<? extends Parcelable>) buildModelPhoto());
                ActivityViewPager.imageSize = new ImageSize(holder.bigImg.getMeasuredWidth()
                        , holder.bigImg.getMeasuredHeight());
                context.startActivity(i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return imgList.size();
    }
}

class BigImageViewHolder extends RecyclerView.ViewHolder {

    ImageView bigImg;

    public BigImageViewHolder(View itemView, Context context) {
        super(itemView);
        bigImg = (ImageView) itemView.findViewById(R.id.big_img);
        ViewGroup.LayoutParams layoutParams = bigImg.getLayoutParams();
        layoutParams.width = context.getResources().getDisplayMetrics().widthPixels;
        layoutParams.height = layoutParams.width * 9 / 16;
        bigImg.setLayoutParams(layoutParams);
    }
}
